package com.example.blogserver.service.Impl;

import com.example.blogserver.a_utils.Constant;
import com.example.blogserver.a_utils.MqConstants;
import com.example.blogserver.a_utils.ResponseBodyMessage;
import com.example.blogserver.mapper.ArticleMapper;
import com.example.blogserver.mapper.UserMapper;
import com.example.blogserver.pojo.Article;
import com.example.blogserver.pojo.User;
import com.example.blogserver.service.EditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressWarnings({"all"})
@Slf4j
@Service
public class EditServiceImpl implements EditService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 获取用户信息
     */
    @Override
    public User getUser(String account) {
        User user = userMapper.queryUser(account);
        return user;
    }

    /**
     * 新增文章
     */
    @Override
    public ResponseBodyMessage<Boolean> addArticle(HttpServletRequest request, String titled, String contentd) {
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        //获取文章发布作者信息
        String account = (String) request.getSession(false).getAttribute(Constant.USERINFO_SESSION_KEY);
        //获取作者的userId
        User user = getUser(account);
        String userId = user.getUserid();
        //判断为新增文章还是修改文章
        String blogId = request.getParameter("blogId");
        if (blogId == null || blogId.equals("")) {
            //说明为新增文章
            Article article = new Article();
            article.setUserId(userId);
            article.setTitle(titled);
            article.setContent(contentd);
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sf.format(new Date());
            article.setPostTime(time);
            int res = articleMapper.addArticle(article);
            int kvl = addTextCount(userId);
            //对作者的审核中文章数进行修改
            userMapper.addUserSumExamine(userId);
            if (res == 1 && kvl == 1) {
                rabbitTemplate.convertAndSend(MqConstants.ARTICLE_EXCHANGE, MqConstants.ARTICLE_INSERT_KEY, article.getId());
                responseBodyMessage.setStatus(1);
                responseBodyMessage.setMessage("文章发布完成，请等待管理员进行审核~");
                responseBodyMessage.setData(true);
            } else {
                responseBodyMessage.setStatus(-1);
                responseBodyMessage.setMessage("文章发布失败~");
                responseBodyMessage.setData(false);
            }
        } else {
            //说明为修改文章
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sf.format(new Date());
            int res = updateArticle(blogId, titled, contentd, time);
            //对作者的已发布文章数和审核量进行修改 已发布-1 审核中+1 并修改文章状态
            Article article = articleMapper.getArticleByBlogId(blogId);
            if (article.getStatus() == 0 || article.getStatus() == 2) {
                if (article.getStatus() == 2) {
                    //审批失败，进行修改发布的
                    //审批失败-1 审批中+1
                    userMapper.redUserSumFail(article.getUserId());
                    userMapper.addUserSumExamine(article.getUserId());
                } else {
                    //审批中的 不做处理
                }
            } else {
                //已发布的
                //已发布数量-1
                userMapper.redUserSumPublished(userId);
                //审核数量+1
                userMapper.addUserSumExamine(userId);
            }
            articleMapper.updateArticleStatus(blogId, 0);
            if (res == 1) {
                rabbitTemplate.convertAndSend(MqConstants.ARTICLE_EXCHANGE, MqConstants.ARTICLE_INSERT_KEY, article.getId());
                responseBodyMessage.setStatus(1);
                responseBodyMessage.setMessage("修改文章完成，请等待管理员进行审核~");
                responseBodyMessage.setData(true);
            } else {
                responseBodyMessage.setStatus(-1);
                responseBodyMessage.setMessage("修改文章失败~");
                responseBodyMessage.setData(false);
            }
        }
        return responseBodyMessage;
    }

    /**
     * 根据博客id获取博客信息
     */
    @Override
    public Article getArticleByBlogId(String blogId) {
        Article article = articleMapper.getArticleByBlogId(blogId);
        return article;
    }

    /**
     * 对用户的文章数进行+1
     */
    @Override
    public int addTextCount(String userId) {
        return userMapper.addTextCount(userId);
    }

    /**
     * 对文章进行修改
     */
    @Override
    public int updateArticle(String blogId, String titled, String contentd, String time) {
        rabbitTemplate.convertAndSend(MqConstants.ARTICLE_EXCHANGE, MqConstants.ARTICLE_INSERT_KEY, blogId);
        return articleMapper.updateArticle(blogId, titled, contentd, time);
    }
}
